layui.define(function(exports){ //提示：模块也可以依赖其它模块，如：layui.define('layer', callback);
  var sysResourcePage = {
    render: function(){
    	var $ = layui.$,
    	table = layui.table,
    	layer = layui.layer,
    	form = layui.form,
    	tree = layui.tree,
    	util = layui.util,
    	laydate = layui.laydate;
    	var treeObject;
    	loadTree();
    	setFullHeight('treePanel');
    	
    	$('#refresh-opt').click(function(){
    		loadTree();
    	});
    	
    	form.on('submit(treeform-submit)', function(data){
    		$.ajax({
  			    type: 'POST',
  			    url: '/boss/sys_resource/update_item',
  			    data: data.field,
  			    success: function (data2) {
  			        if(data2.success){
  			        	layer.msg(data2.message);
  			        	loadTree();
//  				        var node = $('div[data-id='+data.field.id+']');
//  				        $(node).find('div.layui-tree-entry:first').find('span.layui-tree-txt').text(data.field.name);
  			        }else{
  			            layer.msg(data.message,{icon: 5,anim: 6,time: 0,btn:['确定']});
  			        }
  			    },
  			    error: function(data) {
  			        layer.msg(data.responseJSON.message,{icon: 5,anim: 6,time: 0,btn:['确定']});
  			    },
  			    dataType: "json"
  			});
    	});
    	
    	util.event('batch-del-opt', {
			deleteNodeChecked: function(othis){
		      var checkedData = treeObject.getChecked('treePanel'); //获取选中节点的数据
		      var keys = new Array();
		      getSelected(keys,checkedData);
		      batchDelete(keys);
		    }
		  });
    	
    	function getSelected(keys,checkedData){
    		$.each(checkedData,function(i,item){
	  			if(item.children.length > 0){
	  				getSelected(keys,item.children);
	  			} else {
	  				keys.push(item.id);
	  			}
	  		});
    	}
    	
    	function setFullHeight(id){
			var setEleH = function(){
				var w_h = window.innerHeight
				var e_h = w_h - 30 - 20 - 73
				document.getElementById(id).style.height = e_h+'px'
				document.getElementById(id).style.overflowY = 'auto'
			}
			window.onresize = function(){  
			  setEleH();
			}
			setEleH();
		}
    	
    	function batchDelete(keys){
    		$.ajax({
	                type: 'POST',
	                url: '/boss/sys_resource/delete_item',
	                data: JSON.stringify(keys),
	                success: function (data) {
	                    if(data.success){
	                    	loadTree();
	                    }else{
	                        layer.msg(data.message,{icon: 5,anim: 6,time: 0,btn:['确定']});
	                    }
	                    tableObj.reload();
	                    layer.msg(data.message);
	                },
	                error: function(data) {
	                    layer.msg(data.responseJSON.message,{icon: 5,anim: 6,title:'错误',time: 0,btn:['确定']});
	                },
	                contentType: "application/json",
	                dataType: "json"
	            });
    	}
    	
    	$('#save-opt').click(function(){
    		$('#treeform-submit').trigger('click');
    	});
    	
    	function loadTree(){
    		  $.post(
    			'/boss/sys_resource/tree_data_source',
    		    null,
    		    function(data, textStatus, jqXHR){
    				treeObject = tree.render({
    		    	    elem : '#treePanel',
    		    	    data : data,
    		    	    showCheckbox : true,
    		    	    accordion : false,
    		    	    onlyIconControl : true,
    		    	    edit : ['add', 'del'],
    		    	    text : {
		    	    	  defaultNodeName: '[新建节点]' //节点默认名称
		    	    	  ,none: '无数据' //数据为空时的提示文本
		    	    	},
    		    	    click : function(obj){
    		    	    	form.val("treeform",obj.data);
    		    	    },
    		    	    operate: function(obj){
    		    	    	var type = obj.type; //得到操作类型：add、edit、del
    		    	        var data = obj.data; //得到当前节点的数据
    		    	        var elem = obj.elem; //得到当前节点元素
    		    	        var id = data.id; //得到节点索引
    		    	        if(type === 'add'){ //增加节点
    		    	          var addNodeId = 0;
    		    	          $.ajax({
    		    	        	    type: 'POST',
    		    	        	    url: '/boss/sys_resource/add_item',
    		    	        	    async : false,
    		    	        	    data : {
    	    	        		    	parentResourceId:data.id,
    	    	        		    	name:'[新建节点]',
    	    	        		    },
    		    	        	    success: function (data) {
    		    	        	        if(data.success){
    		    	        	        	addNodeId = data.data.id;
    		    	        	        	loadTree();
    		    	        	        }else{
    		    	        	            layer.msg(data.message,{icon: 5,anim: 6,time: 0,btn:['确定']});
    		    	        	        }
    		    	        	    },
    		    	        	    error: function(data) {
    		    	        	        layer.msg(data.responseText,{icon: 5,anim: 6,time: 0,btn:['确定']});
    		    	        	    },
    		    	        	    dataType: "json"
    		    	        	});
    		    	          return addNodeId;
    		    	        } else if(type === 'del'){ //删除节点
    		    	          var keys = new Array();
    		    	          keys.push(data.id);
    		    	          batchDelete(keys);
    		    	        };
    		    	    }
    		    	  });
    		    },"json");
    	  }
		  $('#treePanel').on('click','.layui-icon-subtraction',function(){
  			$.post(
    			'/boss/sys_resource/update_item',
    		    {
    				id:$(this).parent().next('input').val(),
    				name:$(this).parent().nextAll('.layui-tree-txt').text(),
    				spread:true
    			},
    		    null,"json");
		  })
		  $('#treePanel').on('click','.layui-icon-addition',function(){
			  $.post(
    			'/boss/sys_resource/update_item',
    		    {
    				id:$(this).parent().next('input').val(),
    				name:$(this).parent().nextAll('.layui-tree-txt').text(),
    				spread:false
    			},null,"json");
		  })
    }
  };
  
  
  
  //输出test接口
  exports('sysResourcePage', sysResourcePage);
});